package com.ruoyi.loupan.controller;

import java.time.Duration;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.loupan.domain.*;
import com.ruoyi.loupan.service.*;
import com.ruoyi.system.api.RemoteFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 楼盘信息Controller
 * 
 * @author wish
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/info")
public class TbPremisesInfoController extends BaseController
{
    @Autowired
    private ITbPremisesInfoService tbPremisesInfoService;

    @Autowired
    private ISysRegionService regionService;

    @Autowired
    private ITbSubwayService subwayService;

    @Autowired
    private ITbBusinessCircleService businessCircleService;

    @Autowired
    private ITbConfigPremisesPropertyService premisesPropertyService;

    @Autowired
    private ITbRelationPremisesPropertyService relationPremisesPropertyService;

    @Autowired
    private ITbPremisesFacilityService premisesFacilityService;

    @Autowired
    private ITbAlbumService tbAlbumService;

    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 查询楼盘信息列表
     */
    @PreAuthorize(hasPermi = "system:info:list")
    @GetMapping("/list")
    public TableDataInfo list(TbPremisesInfo tbPremisesInfo)
    {
        startPage();
        List<TbPremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        return getDataTable(list);
    }

    /**
     * 导出楼盘信息列表
     */
    @PreAuthorize(hasPermi = "system:info:export")
    @Log(title = "楼盘信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbPremisesInfo tbPremisesInfo) throws IOException
    {
        List<TbPremisesInfo> list = tbPremisesInfoService.selectTbPremisesInfoList(tbPremisesInfo);
        ExcelUtil<TbPremisesInfo> util = new ExcelUtil<TbPremisesInfo>(TbPremisesInfo.class);
        util.exportExcel(response, list, "楼盘信息数据");
    }

    /**
     * 获取楼盘信息详细信息
     */
    @PreAuthorize(hasPermi = "system:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbPremisesInfoService.selectTbPremisesInfoById(id));
    }

    /**
     * 新增楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:addloupan")
    @Log(title = "楼盘信息", businessType = BusinessType.INSERT)
    @PostMapping("addloupan")
    public int add(@RequestBody TbPremisesInfo tbPremisesInfo)
    {
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(tbPremisesInfo.getUuid(), "", Duration.ofSeconds(15));
        if (!aBoolean){
            return 3;
        }else {
            System.out.println("添加楼盘form表单:"+tbPremisesInfo.toString());
            int i = tbPremisesInfoService.insertTbPremisesInfo(tbPremisesInfo);
            System.out.println("主键id:"+tbPremisesInfo.getId());
            for (Long property : tbPremisesInfo.getPropertys()) {
                TbRelationPremisesProperty tbRelationPremisesProperty = new TbRelationPremisesProperty();
                tbRelationPremisesProperty.setPropertyId(property);
                tbRelationPremisesProperty.setPremisesId(tbPremisesInfo.getId());
                relationPremisesPropertyService.insertTbRelationPremisesProperty(tbRelationPremisesProperty);
                System.out.println("特色ok:"+tbRelationPremisesProperty.toString());
            }
            for (TbPremisesFacility tbPremisesFacility : tbPremisesInfo.getJiaotonglist()) {
                tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
                tbPremisesFacility.setType(1);
                premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
                System.out.println("交通ok:"+tbPremisesFacility.toString());
            }
            for (TbPremisesFacility tbPremisesFacility : tbPremisesInfo.getZhoubianlist()) {
                tbPremisesFacility.setPremisesId(tbPremisesInfo.getId());
                tbPremisesFacility.setType(0);
                premisesFacilityService.insertTbPremisesFacility(tbPremisesFacility);
                System.out.println("周边ok:"+tbPremisesFacility.toString());
            }
            if (tbPremisesInfo.getField1()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField1());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(1L);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);

            }
            if (tbPremisesInfo.getField2()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField2());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(2L);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField3()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField3());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(3L);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField4()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField4());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(4L);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            if (tbPremisesInfo.getField5()!=""){
                TbAlbum tbAlbum = new TbAlbum();
                tbAlbum.setUrl(tbPremisesInfo.getField5());
                tbAlbum.setPremisesId(tbPremisesInfo.getId());
                tbAlbum.setAlbumType(5L);
                tbAlbum.setName("file");
                tbAlbumService.insertTbAlbum(tbAlbum);
            }
            return i;
        }

    }

    /**
     * 修改楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:edit")
    @Log(title = "楼盘信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPremisesInfo tbPremisesInfo)
    {
        return toAjax(tbPremisesInfoService.updateTbPremisesInfo(tbPremisesInfo));
    }

    /**
     * 删除楼盘信息
     */
    @PreAuthorize(hasPermi = "system:info:remove")
    @Log(title = "楼盘信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbPremisesInfoService.deleteTbPremisesInfoByIds(ids));
    }
    //省
    @PreAuthorize(hasPermi = "system:info:getsheng")
    @GetMapping("getsheng")
    public List<SysRegion> getsheng(){
        return regionService.getsheng();
    }
    //市
    @PreAuthorize(hasPermi = "system:info:getshi")
    @GetMapping("getshi")
    public List<SysRegion> getshi(TbPremisesInfo tbPremisesInfo){
        return regionService.getshi(tbPremisesInfo.getProvince());
    }
    //县
    @PreAuthorize(hasPermi = "system:info:getxian")
    @GetMapping("getxian")
    public List<SysRegion> getxian(TbPremisesInfo tbPremisesInfo){
        return regionService.getshi(tbPremisesInfo.getCity());
    }
    //获取地铁
    @PreAuthorize(hasPermi = "system:info:getsubway")
    @GetMapping("getsubway")
    public List<TbSubway> getsubway(TbPremisesInfo tbPremisesInfo){
        return subwayService.getsubway(tbPremisesInfo.getProvince());
    }
    //获取商圈
    @PreAuthorize(hasPermi = "system:info:getarea")
    @GetMapping("getarea")
    public List<TbBusinessCircle> getarea(TbPremisesInfo tbPremisesInfo){
        return businessCircleService.getarea(tbPremisesInfo.getCounty());
    }
    //获取商圈
    @PreAuthorize(hasPermi = "system:info:getproperty")
    @GetMapping("getproperty")
    public List<TbBusinessCircle> getproperty(){
        return premisesPropertyService.getproperty();
    }

    @PreAuthorize(hasPermi = "system:info:getuuid")
    @GetMapping("getuuid")
    public String getuuid(){
        return  UUID.randomUUID()+"";
    }
}
